/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bsu.android.gtv.apps.bconnected.parser;

import edu.bsu.android.gtv.apps.bconnected.objects.Tutor;
import edu.bsu.android.gtv.apps.bconnected.objects.SISession;
import edu.bsu.android.gtv.apps.bconnected.objects.Connection;
import edu.bsu.android.gtv.apps.bconnected.objects.Category;
import edu.bsu.android.gtv.apps.bconnected.objects.Content;
import edu.bsu.android.gtv.apps.bconnected.objects.Course;
import edu.bsu.android.gtv.apps.bconnected.objects.Degree;
import edu.bsu.android.gtv.apps.bconnected.objects.Event;
import edu.bsu.android.gtv.apps.bconnected.objects.EventType;
import edu.bsu.android.gtv.apps.bconnected.objects.Housing;
import edu.bsu.android.gtv.apps.bconnected.objects.Organization;
import edu.bsu.android.gtv.apps.bconnected.objects.Person;
import edu.bsu.android.gtv.apps.bconnected.objects.Term;

import java.util.ArrayList;
import java.util.List;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

/**
 * Android SAX parser for the XML feeds from the .NET service handler.  Each class parses a specific feed and loads the 
 * appropriate object including:
 * {@link Category}
 * {@link Content}
 * {@link Course}
 * {@link Degree}
 * {@link Event}
 * {@link Housing}
 * {@link Organization}
 * {@link Person}
 * {@link Term}
 * 
 * Samples of the XML feeds from the service handler can be found under the root folder - labeled "sample xml".  Caching is handled
 * by the .NET application, both at the XML doc level and the database.  Depending on the type of data being returned, the XML doc
 * may be cached by the web server for up to 24 hours.  
 * 
 * @author Kyle Parker, University Computing Services
 */
public class AndroidSaxServiceParser extends BaseServiceParser {
	public AndroidSaxServiceParser(String url, List<String> querystringParms) {
		super(url, querystringParms);
	}

	public ArrayList<Category> parseCategory() {
		final Category currentItem = new Category();
		final ArrayList<Category> items = new ArrayList<Category>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(CATEGORY);

		item.setEndElementListener(new EndElementListener() {
			public void end()  {
				items.add(currentItem.copy());
			}
		});

		item.getChild(CATEGORY_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCategoryID(body);
			}
		});

		item.getChild(CATEGORY_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCategoryName(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return items;
	}

	public ArrayList<Content> parseContent() {
		final Content currentItem = new Content();
		final ArrayList<Content> items = new ArrayList<Content>();

        RootElement root = new RootElement(ROOT);
        Element item = root.getChild(CONTENT);
        
        item.setEndElementListener(new EndElementListener() {
			public void end() {
				items.add(currentItem.copy());
			}
		}); 

        item.getChild(CATEGORY_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCategoryID(body);
			}
		});

        item.getChild(CATEGORY_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCategoryName(body);
			}
		});
		
        item.getChild(CONTENT_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body)  {
				currentItem.setContentID(body);
			}
		});

        item.getChild(FULL_STORY_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setFullStoryURL(body);
			}
		});

        item.getChild(HEADLINE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setHeadline(body);
			}
		});

        item.getChild(TAGLINE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTagline(body);
			}
		});

        item.getChild(THUMB_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setThumbURL(body);
			}
		});
		
		try {		
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return items;
	}
	
	public ArrayList<Connection> parseConnection() {
		final Connection currentItem = new Connection();
		final ArrayList<Connection> items = new ArrayList<Connection>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(CONNECTION);

		item.setEndElementListener(new EndElementListener() {
			public void end()  {
				items.add(currentItem.copy());
			}
		});

		item.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setError(body);
			}
		});

		item.getChild(SUCCESS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setSuccess(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return items;
	}
	
	public ArrayList<Course> parseCourse() {
		final Course currentItem = new Course();
		final ArrayList<Course> items = new ArrayList<Course>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(COURSE);

		item.setEndElementListener(new EndElementListener() {
			public void end() {
				items.add(currentItem.copy());
			}
		});

		item.getChild(BUILDING_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBuildingDesc(body);
			}
		});

		item.getChild(COMMENTS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setComments(body);
			}
		});

		item.getChild(COURSE_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseDesc(body);
			}
		});

		item.getChild(COURSE_NUM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseNum(body);
			}
		});
		
		item.getChild(COURSE_REF_NUM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseRefNum(body);
			}
		});
		
		item.getChild(COURSE_WITH_TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseWithTitle(body);
			}
		});
		
		item.getChild(CREDIT_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCreditType(body);
			}
		});
		
		item.getChild(CR_NC_A).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCrNcA(body);
			}
		});
		
		item.getChild(DAY_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDayTime(body);
			}
		});

		item.getChild(DISCIPLINE_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDisciplineCode(body);
			}
		});

		item.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setError(body);
			}
		});

		item.getChild(FINAL_EXAM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setFinalExam(body);
			}
		});

		item.getChild(FULL_TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setFullTitle(body);
			}
		});

		item.getChild(HOURS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setHours(body);
			}
		});

		item.getChild(IMAGE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setImageURL(body);
			}
		});

		item.getChild(LATITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLatitude(body);
			}
		});

		item.getChild(LOCATION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLocation(body);
			}
		});
		
		item.getChild(LOCATION_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLocationURL(body);
			}
		});
		
		item.getChild(LONGITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLongitude(body);
			}
		});

		item.getChild(REPEAT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setRepeat(body);
			}
		});

		item.getChild(SECTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setSection(body);
			}
		});
		
		item.getChild(TERM_BEGIN_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermBeginDate(body);
			}
		});
		
		item.getChild(TERM_END_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermEndDate(body);
			}
		});

		item.getChild(TERM_FULL_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermFullDesc(body);
			}
		});
		
		item.getChild(TERM_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermID(body);
			}
		});
		
		item.getChild(TERM_NUMBER).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermNumber(body);
			}
		});
		
		item.getChild(TERM_YEAR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermYear(body);
			}
		});
		
		item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTitle(body);
			}
		});
		
		item.getChild(TOTAL_CREDIT_HOURS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTotalCreditHours(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}

		return items;
	}

	public ArrayList<Degree> parseDegree() {
		final Degree currentItem = new Degree();
		final ArrayList<Degree> items = new ArrayList<Degree>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(DEGREE);

		item.setEndElementListener(new EndElementListener() {
			public void end()  {
				items.add(currentItem.copy());
			}
		});

		item.getChild(DEGREE_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDegreeType(body);
			}
		});

		item.getChild(DEPARTMENT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDepartment(body);
			}
		});
		
		item.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setError(body); 
			}
		});

		item.getChild(PROGRAM_OPTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setProgramOption(body);
			}
		});

		item.getChild(PROGRAM_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setProgramType(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return items;
	}
		
	public ArrayList<Event> parseEvent() {
		final Event currentItem = new Event();
		final ArrayList<Event> items = new ArrayList<Event>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(EVENT);

		item.setEndElementListener(new EndElementListener() {
			public void end() {
				items.add(currentItem.copy());
			}
		}); 

		item.getChild(AUDIENCE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setAudience(body);
			}
		});

		item.getChild(BEGIN_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBeginDate(body);
			}
		});

		item.getChild(BEGIN_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBeginTime(body);
			}
		});

		item.getChild(BUILDING_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBuildingCode(body);
			}
		});

		item.getChild(BUILDING_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBuildingDesc(body);
			}
		});

		item.getChild(CALENDAR_EVENT_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCalendarEventID(body);
			}
		});

		item.getChild(CALENDAR_EVENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCalendarEventName(body);
			}
		});

		item.getChild(CONTACT_EMAIL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setContactEmail(body);
			}
		});

		item.getChild(CONTACT_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setContactName(body);
			}
		});

		item.getChild(CONTACT_PHONE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setContactPhone(body);
			}
		});

		item.getChild(COST).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCost(body);
			}
		});

		item.getChild(END_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEndDate(body);
			}
		});

		item.getChild(END_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEndTime(body);
			}
		});

		item.getChild(EVENT_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEventDesc(body);
			}
		}); 

		item.getChild(EVENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEventName(body);
			}
		});

		item.getChild(LATITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLatitude(body);
			}
		});

		item.getChild(IMAGE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setImageURL(body);
			}
		});

		item.getChild(LOCATION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLocation(body);
			}
		});

		item.getChild(LONGITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLongitude(body);
			}
		}); 

		item.getChild(SHARE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setShareURL(body);
			}
		});

		item.getChild(TERM_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermID(body);
			}
		});

		item.getChild(UNIVERSITY_CALENDAR_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setUniversityCalendarID(body);
			}
		});
		
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return items;
	}
	
	public ArrayList<EventType> parseEventType() {
		final EventType currentItem = new EventType();
		final ArrayList<EventType> items = new ArrayList<EventType>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(EVENT_TYPE);

		item.setEndElementListener(new EndElementListener() {
			public void end()  {
				items.add(currentItem.copy());
			}
		});

		item.getChild(CALENDAR_EVENT_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCalendarEventID(body);
			}
		});

		item.getChild(CALENDAR_EVENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCalendarEventName(body);
			}
		});

		item.getChild(EVENT_COUNT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEventCount(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return items;
	}
	
	public ArrayList<Housing> parseHousing() {
		final Housing currentItem = new Housing();
		final ArrayList<Housing> items = new ArrayList<Housing>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(HOUSING); 

		item.setEndElementListener(new EndElementListener() {
			public void end() {
				items.add(currentItem.copy());
			}
		});

		item.getChild(COMPLEX_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setComplexDesc(body);
			}
		});

		item.getChild(COMPLEX_FEATURES).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setComplexFeatures(body);
			}
		});

		item.getChild(COMPLEX_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setComplexName(body);
			}
		});
		
		item.getChild(CONTRACT_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setContractType(body);
			}
		});
		
		item.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setError(body); 
			}
		});
		
		item.getChild(FLOOR_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setFloorCode(body);
			}
		});
		
		item.getChild(FLOOR_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setFloorDesc(body);
			}
		});
		
		item.getChild(HALL_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setHallCode(body);
			}
		});
		
		item.getChild(HALL_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setHallDesc(body);
			}
		});

		item.getChild(HALL_GROUP_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setHallGroupDesc(body);
			}
		});

		item.getChild(HOUSING_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setHousingType(body);
			}
		});

		item.getChild(IMAGE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setImageURL(body);
			}
		});

		item.getChild(LATITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLatitude(body);
			}
		});

		item.getChild(LIVING_LEARNING).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLivingLearning(body);
			}
		});

		item.getChild(LONGITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLongitude(body);
			}
		});

		item.getChild(MEAL_PLAN).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setMealPlan(body);
			}
		});

		item.getChild(RATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setRate(body);
			}
		});
		
		item.getChild(ROOM_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setRoomDesc(body);
			}
		});
		
		item.getChild(ROOM_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setRoomID(body);
			}
		});

		item.getChild(ROOM_NUMBER).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setRoomNumber(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}

		return items;
	}
	
	public ArrayList<Organization> parseOrganization() {
		final Organization currentItem = new Organization();
		final ArrayList<Organization> items = new ArrayList<Organization>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(ORGANIZATION);

		item.setEndElementListener(new EndElementListener() {
			public void end() {
				items.add(currentItem.copy());
			}
		});

		item.getChild(ADDRESS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setAddress(body);
			}
		});

		item.getChild(CATEGORY).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCategory(body);
			}
		});

		item.getChild(DEGREE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDegree(body);
			}
		});

		item.getChild(DISPLAY_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDisplayName(body);
			}
		});

		item.getChild(DUES).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDues(body);
			}
		});

		item.getChild(EMAIL_ADDRESS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEmailAddress(body);
			}
		});

		item.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setError(body);
			}
		});

		item.getChild(FOUNDED).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setFounded(body);
			}
		});
		
		item.getChild(MEMBER_COUNT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setMemberCount(body);
			}
		});
		
		item.getChild(MEMBERSHIP_REQ).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setMembershipReq(body);
			}
		});
		
		item.getChild(ORGANIZATION_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setOrganizationDesc(body);
			}
		});
		
		item.getChild(ORGANIZATION_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setOrganizationID(body);
			}
		});
		
		item.getChild(ORGANIZATION_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setOrganizationName(body);
			}
		});
		
		item.getChild(PHONE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setPhone(body);
			}
		});
		
		item.getChild(PROGRAM_OPTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setProgramOption(body);
			}
		});

		item.getChild(PROGRAM_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setProgramType(body);
			}
		});

		item.getChild(SHARE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setShareURL(body);
			}
		});

		item.getChild(WEBSITE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setWebsite(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}

		return items;
	}

	public ArrayList<Person> parsePerson() {
		final Person currentItem = new Person();
		final ArrayList<Person> items = new ArrayList<Person>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(PERSON);

		item.setEndElementListener(new EndElementListener() {
			public void end() {
				items.add(currentItem.copy());
			}
		});

		item.getChild(ADDRESS_1).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setAddress1(body);
			}
		});

		item.getChild(ADDRESS_2).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setAddress2(body);
			}
		});

		item.getChild(ADVISING_CENTER).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setAdvisingCenter(body);
			}
		});

		item.getChild(ADVISOR_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setAdvisorName(body);
			}
		});

		item.getChild(ADVISOR_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setAdvisorType(body);
			}
		});

		item.getChild(BSU_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBSUID(body);
			}
		});

		item.getChild(BUILDING_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBuildingCode(body);
			}
		});

		item.getChild(BUILDING_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBuildingDesc(body);
			}
		});

		item.getChild(CITY).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCity(body);
			}
		});

		item.getChild(CONNECTION_TYPE_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setConnectionTypeID(body);
			}
		});

		item.getChild(CONNECTION_TYPE_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setConnectionTypeName(body);
			}
		});
		
		item.getChild(COURSE_COUNT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseCount(body);
			}
		});

		item.getChild(COURSE_EMAIL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseEmail(body);
			}
		});
		
		item.getChild(COURSE_INFO).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseInfo(body);
			}
		});

		item.getChild(COURSE_NUM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseNum(body);
			}
		});

		item.getChild(COURSE_REF_NUM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseRefNum(body);
			}
		});

		item.getChild(COURSE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseURL(body);
			}
		});

		item.getChild(COURSE_WITH_TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseWithTitle(body);
			}
		});

		item.getChild(DAY_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDayTime(body);
			}
		});

		item.getChild(DEPARTMENT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDepartment(body);
			}
		});

		item.getChild(DISCIPLINE_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDisciplineCode(body);
			}
		});

		item.getChild(EMAIL_ADDRESS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEmailAddress(body);
			}
		});

		item.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setError(body);
			}
		});

		item.getChild(FIRST_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setFirstName(body);
			}
		});

		item.getChild(LAST_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLastName(body);
			}
		});

		item.getChild(LATITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLatitude(body);
			}
		});

		item.getChild(LOGIN_GUID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLoginGuid(body);
			}
		});

		item.getChild(LONGITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLongitude(body);
			}
		});

		item.getChild(MEMBER_ROLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setMemberRole(body);
			}
		});

		item.getChild(MEMBER_ROLE_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setMemberRoleID(body);
			}
		});

		item.getChild(MEMBER_TYPE_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setMemberTypeID(body);
			}
		});

		item.getChild(MIDDLE_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setMiddleName(body);
			}
		});

		item.getChild(NAME_PREFIX).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setNamePrefix(body);
			}
		});

		item.getChild(NAME_SUFFIX).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body)  {
				currentItem.setNameSuffix(body);
			}
		});

		item.getChild(OFFICE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body)  {
				currentItem.setOffice(body);
			}
		});

		item.getChild(PERSON_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setPersonType(body);
			}
		});

		item.getChild(PHONE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setPhone(body);
			}
		});

		item.getChild(PRIVACY_RESTRICTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setPrivacyRestriction(body);
			}
		});

		item.getChild(ROLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setRole(body);
			}
		});

		item.getChild(ROOM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setRoom(body);
			}
		});

		item.getChild(SECTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setSection(body);
			}
		});

		item.getChild(STARRED).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setStarred(Boolean.parseBoolean(body));
			}
		});

		item.getChild(STATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setState(body);
			}
		});

		item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseTitle(body);
			}
		});

		item.getChild(TITLE_RANK).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTitleRank(body);
			}
		});

		item.getChild(ZIP).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setZip(body);
			}
		});
		
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return items;
	}
	
	public ArrayList<SISession> parseSISession() {
		final SISession currentItem = new SISession();
		final ArrayList<SISession> items = new ArrayList<SISession>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(SI_SESSION);

		item.setEndElementListener(new EndElementListener() {
			public void end()  {
				items.add(currentItem.copy());
			}
		});

		item.getChild(BEGIN_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBeginTime(body);
			}
		});

		item.getChild(BUILDING_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBuildingCode(body);
			}
		});

		item.getChild(BUILDING_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBuildingDesc(body);
			}
		});

		item.getChild(DAY_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDay(body);
			}
		});

		item.getChild(END_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEndTime(body);
			}
		});

		item.getChild(LOCATION_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLocationID(body);
			}
		});

		item.getChild(ROOM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setRoom(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return items;
	}
	
	public ArrayList<Term> parseTerm() {
		final Term currentItem = new Term();
		final ArrayList<Term> items = new ArrayList<Term>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(TERM);

		item.setEndElementListener(new EndElementListener() {
			public void end()  {
				items.add(currentItem.copy());
			}
		});

		item.getChild(END_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEndDate(body);
			}
		});

		item.getChild(SHORT_ACADEMIC_YEAR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setShortAcademicYear(body);
			}
		});

		item.getChild(SHORT_TERM_FULL_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setShortTermFullDesc(body);
			}
		});

		item.getChild(START_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setStartDate(body);
			}
		});

		item.getChild(TERM_NUMBER).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermNumber(body);
			}
		});

		item.getChild(TERM_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermID(body);
			}
		});

		item.getChild(TERM_FULL_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTermFullDesc(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return items;
	}
	
	public ArrayList<Tutor> parseTutor() {
		final Tutor currentItem = new Tutor();
		final ArrayList<Tutor> items = new ArrayList<Tutor>();
		
		RootElement root = new RootElement(ROOT);
		Element item = root.getChild(TUTOR);

		item.setEndElementListener(new EndElementListener() {
			public void end()  {
				items.add(currentItem.copy());
			}
		});

		item.getChild(BSU_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBSUID(body);
			}
		});

		item.getChild(BUILDING_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBuildingCode(body);
			}
		});

		item.getChild(BUILDING_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setBuildingDesc(body);
			}
		});

		item.getChild(CONNECTION_TYPE_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setConnectionTypeID(body);
			}
		});

		item.getChild(CONNECTION_TYPE_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setConnectionTypeName(body);
			}
		});

		item.getChild(COURSE_NUM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setCourseNum(body);
			}
		});

		item.getChild(DEPARTMENT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDepartment(body);
			}
		});

		item.getChild(DISCIPLINE_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setDisciplineCode(body);
			}
		});

		item.getChild(EMAIL_ADDRESS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setEmailAddress(body);
			}
		});

		item.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setError(body);
			}
		});

		item.getChild(FIRST_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setFirstName(body);
			}
		});

        item.getChild(FULL_STORY_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setFullStoryURL(body);
			}
		});

        item.getChild(HEADLINE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setHeadline(body);
			}
		});

		item.getChild(IMAGE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setImageURL(body);
			}
		});

		item.getChild(LAST_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLastName(body);
			}
		});

		item.getChild(LATITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLatitude(body);
			}
		});

		item.getChild(LOGIN_GUID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLoginGuid(body);
			}
		});

		item.getChild(LONGITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setLongitude(body);
			}
		});

		item.getChild(MIDDLE_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setMiddleName(body);
			}
		});

		item.getChild(NAME_PREFIX).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setNamePrefix(body);
			}
		});

		item.getChild(NAME_SUFFIX).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body)  {
				currentItem.setNameSuffix(body);
			}
		});

		item.getChild(PERSON_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setPersonType(body);
			}
		});

		item.getChild(PHONE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setPhone(body);
			}
		});

		item.getChild(ROOM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setRoom(body);
			}
		});

		item.getChild(STARRED).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setStarred(Boolean.parseBoolean(body));
			}
		});

		item.getChild(TAGLINE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTagline(body);
			}
		});

		item.getChild(TITLE_RANK).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentItem.setTitleRank(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return items;
	}
}
